package com.zk5_education.service_vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;

import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.zk5_education.ResultCode;
import com.zk5_education.config.ExceptionHandle.MyException;
import com.zk5_education.service_vod.Untils.VideoUntils;
import com.zk5_education.service_vod.entity.EduVideo;
import com.zk5_education.service_vod.init.Videoinit;

import com.zk5_education.service_vod.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sf.jsqlparser.statement.delete.Delete;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;


/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-13
 */
@Service
public class EduVideoServiceImpl implements EduVideoService  {
    @Override
    public void deleteVideo(String videoid) {
     try{
         DefaultAcsClient defaultAcsClient = Videoinit.initVodClient(VideoUntils.KEYID, VideoUntils.KEYSCECRET);
         DeleteVideoRequest request=new DeleteVideoRequest();
         request.setVideoIds(videoid);
         DeleteVideoResponse response = defaultAcsClient.getAcsResponse(request);
     }catch(Exception e){
         e.printStackTrace();
         throw new MyException(ResultCode.ERROR,"删除失败");
     }

    }
//添加视频
    @Override
    public String  addvideo(MultipartFile file) {
        try{
            String filename = file.getOriginalFilename();
            InputStream input=file.getInputStream();
            String title;
            title=filename.substring(0,filename.lastIndexOf("."));
            UploadStreamRequest request=new UploadStreamRequest(VideoUntils.KEYID,VideoUntils.KEYSCECRET,title,filename,input);
            UploadVideoImpl uploader=new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String Videoid=response.getVideoId();

            return Videoid;


        }catch(Exception e){
            throw new MyException(ResultCode.ERROR,"添加失败");

        }
    }
//批量删出视频
    @Override
    public void removerbatch(List<String > list) {

        try{
            String videoid= StringUtils.join(list.toArray(),",");
            DefaultAcsClient defaultAcsClient = Videoinit.initVodClient(VideoUntils.KEYID, VideoUntils.KEYSCECRET);
            DeleteVideoRequest request=new DeleteVideoRequest();
            request.setVideoIds(videoid);
             defaultAcsClient.getAcsResponse(request);
        }catch(Exception e){
            e.printStackTrace();
            throw new MyException(ResultCode.ERROR,"删除失败");
        }
    }
}
